package core.base.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import core.base.page.Page;
import core.util.ParamUtil;
import core.util.StringUtil;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

public class PageHelper {
	private HttpServletRequest request;

	private HttpSession session;

	private String param;

	private String url;
	private String divId;
	private int paramPageSize;
	private int paramPage;

	public PageHelper(HttpServletRequest request) {
		this.request = request;
		this.session = this.request.getSession();
		this.divId = request.getParameter("divid");
		this.paramPageSize = ParamUtil.getInt(request, "size", 10);
		this.paramPage = ParamUtil.getInt(request, "pageNum", 1);
	}

	/**
	 * 用于计算用户自定义每页显示记录数
	 * 
	 * @return
	 */
	public int doCustomPerPage(int defaultPerPage) {
		int perPage = defaultPerPage;
		String cUserPerPage = null;
		String cPerPage = ParamUtil.getString(this.request, "size", "");
		if (!"".equals(cPerPage)) {
			this.session.setAttribute("cPerPage", cPerPage);
		}
		cUserPerPage = (String) this.session.getAttribute("cPerPage");
		if (null != cUserPerPage) {
			perPage = Integer.parseInt(cUserPerPage);
		}
		return perPage;
	}

	public String getPageBreakStr(Page pageObject) {
		int page = 1;
		int total = 0;
		int perPage = 10;
		// 取分页器参数
		page = pageObject.getPageNumber();
		total = pageObject.getTotalCount();
		if (total != 0 || total != 1)
			perPage = pageObject.getPageSize();

		boolean display1 = false;
		boolean display2 = false;
		boolean display3 = false;
		boolean display4 = false;
		boolean isHaveParam = false;

		// 设置URL参数,如果pageObject的ParamName和Url为空,则从request对象中取值
		if (StringUtil.isBlank(pageObject.getUrl())) {
			url = request.getRequestURI();
		} else {
			url = pageObject.getUrl();
		}
		param = pageObject.getParamName();
		if (StringUtil.isBlank(param)) {
			param = request.getQueryString();
			if (!StringUtil.isBlank(param)) {
				if (param.indexOf("&divid=" + divId) != -1) {
					param = param.replace("&divid=" + divId, "");
				}
				if (param.indexOf("divid=" + divId + "&") != -1) {
					param = param.replace("divid=" + divId + "&", "");
				}
				if (param.indexOf("divid=" + divId) != -1) {
					param = param.replace("divid=" + divId, "");
				}
				if (param.indexOf("&size=" + paramPageSize) != -1) {
					param = param.replace("&size=" + paramPageSize, "");
				}
				if (param.indexOf("size=" + paramPageSize + "&") != -1) {
					param = param.replace("size=" + paramPageSize + "&", "");
				}
				if (param.indexOf("size=" + paramPageSize) != -1) {
					param = param.replace("size=" + paramPageSize, "");
				}
				if (param.indexOf("&pageNum=" + paramPage) != -1) {
					param = param.replace("&pageNum=" + paramPage, "");
				}
				if (param.indexOf("pageNum=" + paramPage + "&") != -1) {
					param = param.replace("pageNum=" + paramPage + "&", "");
				}
				if (param.indexOf("pageNum=" + paramPage) != -1) {
					param = param.replace("pageNum=" + paramPage, "");
				}
				isHaveParam = true;
			}
		} else {
			isHaveParam = true;
		}

		// 绝对路径
		String realPath = pageObject.getRealPath();
		if(StringUtil.isBlank(realPath)){
			ServletContext servletContext = request.getSession().getServletContext();
			realPath = servletContext.getRealPath("/");
		}

		// 翻页模板名
		String pageTemplate = pageObject.getPageTemplate();

		int totalPage = (total + perPage - 1) / perPage;

		if (page > totalPage || page < 0) {
			page = 1;
		}
		if (divId != null)
			param = param + "&divid=" + divId;
		
		param = StringUtil.defaultString(param, "");
		
		// 当前页
		StringBuffer currentPageBuffer = new StringBuffer();
		currentPageBuffer.append("pageNum=").append(page);
		if (isHaveParam) {
			currentPageBuffer.append("&" + param);
		}
		
		// 首页
		StringBuffer firstPageBuffer = new StringBuffer();
		if (totalPage > 1 && page > 1) {
			firstPageBuffer.append("pageNum=1");
			if (isHaveParam) {
				firstPageBuffer.append("&" + param);
			}
			display1 = true;
		}

		// 上一页
		StringBuffer perPageBuffer = new StringBuffer();
		if (page > 1) {
			perPageBuffer.append("pageNum=" + (page - 1));
			if (isHaveParam) {
				perPageBuffer.append("&" + param);
			}
			display2 = true;
		}

		// 下一页
		StringBuffer nextPageBuffer = new StringBuffer();
		if (page < totalPage) {
			nextPageBuffer.append("pageNum=" + (page + 1));
			if (isHaveParam) {
				nextPageBuffer.append("&" + param);
			}
			display3 = true;
		}

		// 最后一页
		StringBuffer lastPageBuffer = new StringBuffer();
		if (totalPage > 1) {
			lastPageBuffer.append("pageNum=" + totalPage);
			if (isHaveParam) {
				lastPageBuffer.append("&" + param);
			}
			display4 = true;
		}

		// 每页显示记录数
		StringBuffer numPageBuffer = new StringBuffer();
		numPageBuffer.append("<select id=\"size\" name=\"size\" ");
		numPageBuffer.append(" onchange=\"");
		numPageBuffer.append(" if(this.options[this.selectedIndex]");
		numPageBuffer.append(".value!=''){");
		if (isHaveParam) {
			numPageBuffer.append(" pagination({url:'" + url + "',params:'" + param + "&pageNum=1");
		}else{
			numPageBuffer.append(" pagination({url:'" + url + "',params:'pageNum=1");
		}
		numPageBuffer.append("&size='+this.options[this.selectedIndex].value");
		numPageBuffer.append(",id:'" + divId + "'});}\">");
		for (int i = 1; i <= 4; i++) {
			numPageBuffer.append("<option value=\"" + (i * 10) + "\"");
			if ((i * 10) == perPage) {
				numPageBuffer.append(" selected>" + (i * 10) + "</option>");
			} else {
				numPageBuffer.append(">" + (i * 10) + "</option>");
			}
		}
		numPageBuffer.append("<option value=\"50\"");
		if (50 == perPage) {
			numPageBuffer.append(" selected>50</option>");
		} else {
			numPageBuffer.append(">50</option>");
		}

		numPageBuffer.append("<option value=\"100\"");
		if (90 == perPage) {
			numPageBuffer.append(" selected>100</option>");
		} else {
			numPageBuffer.append(">100</option>");
		}

		numPageBuffer.append("</select>");
		// 跳转到第几页
		StringBuffer jumpPageBuffer = new StringBuffer();
		jumpPageBuffer.append("<input id=\"gopage\" class=\"gopage\" type=\"text\" size=\"5\" value=\"\"  ");
		jumpPageBuffer.append("onkeypress=\"javascript:if (event.keyCode==13){ if ((this.value<1)||(this.value>" + totalPage + ") ){ return;} goPage({url:'" + url + "',params:'" + param + "'");
		jumpPageBuffer.append(",id:'" + divId + "'});} \" />");
		// 跳转的链接
		StringBuffer goToPageBuffer = new StringBuffer();

		goToPageBuffer.append("<a href='#' onclick=\"javascript:goPage({url:'" + url + "',params:'" + param + "'");
		goToPageBuffer.append(",id:'" + divId + "'})\">");
		goToPageBuffer.append("跳转</a>");

		Configuration cfg = new Configuration();
		Template newsTemplate = null;
		String deTemplateString = "";
		try {
			cfg.setDirectoryForTemplateLoading(new File(realPath + "/templates/page"));
			cfg.setObjectWrapper(new DefaultObjectWrapper());

			newsTemplate = cfg.getTemplate(pageTemplate, "UTF-8");

			deTemplateString = newsTemplate.toString();
			deTemplateString = deTemplateString.replace("${currentPageUrlScript}",  "pagination({url:'"+url+"',params:'" + currentPageBuffer.toString() + "',id:'" + divId + "'})");
			deTemplateString = deTemplateString.replace("${total}", String.valueOf(total));
			deTemplateString = deTemplateString.replace("${page}", String.valueOf(page));
			if (totalPage == 0)
				totalPage = 1;
			deTemplateString = deTemplateString.replace("${allPage}", String.valueOf(totalPage));
			if ((display1 == true) && (display2 == true)) {
				deTemplateString = deTemplateString.replace("${firstPageStart}", "<a href=\"javascript:pagination({url:'" + url +"',params:'" + firstPageBuffer.toString() + "',id:'" + divId + "'})\" >");
				deTemplateString = deTemplateString.replace("${firstPageEnd}", "</a>");
			} else {
				deTemplateString = deTemplateString.replace("${firstPageStart}", "");
				deTemplateString = deTemplateString.replace("${firstPageEnd}", "");
			}
			if (display2) {
				deTemplateString = deTemplateString.replace("${perPageStart}", "<a href=\"javascript:pagination({url:'"+url+"',params:'" + perPageBuffer.toString() + "',id:'" + divId + "'})\" >");
				deTemplateString = deTemplateString.replace("${perPageEnd}", "</a>");
			} else {
				deTemplateString = deTemplateString.replace("${perPageStart}", "");
				deTemplateString = deTemplateString.replace("${perPageEnd}", "");
			}
			if (display3) {
				deTemplateString = deTemplateString.replace("${nextPageStart}", "<a href=\"javascript:pagination({url:'"+url+"',params:'" + nextPageBuffer.toString() + "',id:'" + divId + "'})\" >");
				deTemplateString = deTemplateString.replace("${nextPageEnd}", "</a>");
			} else {
				deTemplateString = deTemplateString.replace("${nextPageStart}", "");
				deTemplateString = deTemplateString.replace("${nextPageEnd}", "");
			}
			if ((display3 == true) && (display4 == true)) {
				deTemplateString = deTemplateString.replace("${lastPageStart}", "<a href=\"javascript:pagination({url:'"+url+"',params:'" + lastPageBuffer.toString() + "',id:'" + divId + "'})\" >");
				deTemplateString = deTemplateString.replace("${lastPageEnd}", "</a>");
			} else {
				deTemplateString = deTemplateString.replace("${lastPageStart}", "");
				deTemplateString = deTemplateString.replace("${lastPageEnd}", "");
			}
			deTemplateString = deTemplateString.replace("${numPage}", numPageBuffer.toString());
			deTemplateString = deTemplateString.replace("${jumpPage}", jumpPageBuffer.toString());
			deTemplateString = deTemplateString.replace("${gotoPage}", goToPageBuffer.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return deTemplateString;
	}
}
